package net.lastcoder.ifserver.tool;

import net.lastcoder.ifserver.util.FileUtil;
import net.lastcoder.ifserver.util.QuickCrypter;

import org.junit.Test;

import java.util.Arrays;

/**
 * auth: WenYF
 * date: 2017/10/31
 */
public class FzbLuaEncrypt {

    @Test
    public void startEncrypt() {
        String filePath = "E:\\source\\Ifserver\\src\\res\\fzb\\fzb.lua";

        byte[] data = FileUtil.readFileData(filePath);

        if (data[0] == 'w' && data[1] == 'e' && data[2] == 'n' && data[3] == 'y' && data[4] == 'f') {
            return;
        }

        byte[] newData = new byte[data.length + 5];
        QuickCrypter.encrypt(data);
        System.arraycopy(data, 0, newData, 5, data.length);
        int i = 0;
        newData[i++] = 'w';
        newData[i++] = 'e';
        newData[i++] = 'n';
        newData[i++] = 'y';
        newData[i] = 'f';

        FileUtil.writeData2File(newData, "E:\\source\\Ifserver\\src\\res\\fzb\\fzb.lua");
    }

    @Test
    public void startDecrypt() {
        String filePath = "E:\\source\\Ifserver\\src\\res\\fzb\\fzb.lua";

        byte[] data = FileUtil.readFileData(filePath);

        if (!(data[0] == 'w' && data[1] == 'e' && data[2] == 'n' && data[3] == 'y' && data[4] == 'f')) {
            return;
        }

        byte[] newData = new byte[data.length - 5];
        QuickCrypter.decrypt(data);

        System.arraycopy(data, 5, newData, 0, newData.length);

        FileUtil.writeData2File(newData, "E:\\source\\Ifserver\\src\\res\\fzb\\fzb.lua");
    }
}
